package com.canbot.u05.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.receivers.IntentKey;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.ConstUtils;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.ImageTextButtonInVc;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.HashMap;

/**
 * 自定义开机语音界面
 *
 * @author qiandu
 */
public class StarttheVoiceActivity extends BaseAcitivity
        implements OnClickListener {

        private ImageTextButtonInVc sv_systemvoice, sv_customvoice;

        private ImageButton sv_return;

        private CheckBox sv_switch;

        private Context mContext;

        private LinearLayout sv_llyout_start;

        private LinearLayout sv_llayout_close;

        boolean switchflag = true;

        private String from = "";

        private TextView tvTitle;

        private Button btnComplete;

        private Dialog dialogLoading;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_startthevoice);
                mMicLayout = (RelativeLayout) findViewById(R.id.startvoice);
                mContext = this;
                from = getIntent().getStringExtra("SensorBackActivity");
                initView();

        }

        @Override
        protected void onResume() {
                // TODO Auto-generated method stub
                super.onResume();
                setTitle();// 根据跳转过来的界面设置标题
        }

        private void setTitle() {
                if (from != null && !from.equals("")) {
                        Log.i("test", "感应器");
                        btnComplete.setVisibility(View.VISIBLE);
//                        sv_switch.setVisibility(View.GONE);
                        // 设置选择状态
                        SharedPreferences sp = getSharedPreferences(from,
                                Context.MODE_PRIVATE);
                        setStatus(sp.getString("which", "sys"));// 设置状态

                        if (from.equals(IntentKey.SENSOR_BUTT)) {
                                tvTitle.setText("臀部感应器");
                                // SharedPreferences sp = getSharedPreferences("ivButt",
                                // Context.MODE_PRIVATE);
                                // setStatus(sp.getString("which", "sys"));// 设置状态

                        }
                        else if (from.equals(IntentKey.SENSOR_HEAD)) {
                                tvTitle.setText("头部感应器");
                                // SharedPreferences sp = getSharedPreferences("ivHead",
                                // Context.MODE_PRIVATE);
                                // setStatus(sp.getString("which", "sys"));

                        }
                        else if (from.equals(IntentKey.SENSOR_LEFT_EAR)) {
                                tvTitle.setText("左耳感应器");
                                // SharedPreferences sp = getSharedPreferences("ivLeftEar",
                                // Context.MODE_PRIVATE);
                                // setStatus(sp.getString("which", "sys"));

                        }
                        else if (from.equals(IntentKey.SENSOR_RIGHT_EAR)) {
                                tvTitle.setText("右耳感应器");
                                // SharedPreferences sp = getSharedPreferences("ivRightEar",
                                // Context.MODE_PRIVATE);
                                // setStatus(sp.getString("which", "sys"));

                        }
                        else if (from.equals(IntentKey.SENSOR_NECK)) {
                                tvTitle.setText("脖子感应器");
                                // SharedPreferences sp = getSharedPreferences("ivNeck",
                                // Context.MODE_PRIVATE);
                                // setStatus(sp.getString("which", "sys"));

                        } /*
               * else if (from.equals("ivLeftHand")) { tvTitle.setText("左手感应器");
               * SharedPreferences sp = getSharedPreferences("ivLeftHand",
               * Context.MODE_PRIVATE); setStatus(sp.getString("which", "sys"));
               * 
               * } else if (from.equals("ivRightHand")) {
               * tvTitle.setText("右手感应器"); SharedPreferences sp =
               * getSharedPreferences("ivRightHand", Context.MODE_PRIVATE);
               * setStatus(sp.getString("which", "sys"));
               * 
               * }
               */
                        else if (from.equals(IntentKey.SENSOR_BELLY)) {
                                tvTitle.setText("肚子感应器");
                                // SharedPreferences sp = getSharedPreferences("ivBelly",
                                // Context.MODE_PRIVATE);
                                // setStatus(sp.getString("which", "sys"));

                        }
                        else if (from.equals(IntentKey.SENSOR_HEAD_BACK)) {
                                tvTitle.setText("后脑感应器");
                                // SharedPreferences sp = getSharedPreferences("ivHeadBack",
                                // Context.MODE_PRIVATE);
                                // setStatus(sp.getString("which", "sys"));
                        }else if (from.equals(IntentKey.SENSOR_PEOPLE)) {
                                tvTitle.setText("自定义人体感应语音");
                                // SharedPreferences sp = getSharedPreferences("ivHeadBack",
                                // Context.MODE_PRIVATE);
                                // setStatus(sp.getString("which", "sys"));
                        }

                }
                else {
                        tvTitle.setText("自定义开机语音");
                        SharedPreferences sp = getSharedPreferences("start_voice",
                                Context.MODE_PRIVATE);
                        setStatus(sp.getString("which", "sys"));// 设置按钮选中状态
                        Log.i("test", "开机语音");
                        Log.i("test", "---------->" + sp.getString("which", "sys"));

                        btnComplete.setVisibility(View.GONE);
//                        sv_switch.setVisibility(View.VISIBLE);
//                        sv_switch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//
//                                @Override
//                                public void onCheckedChanged(CompoundButton paramCompoundButton,
//                                                             boolean paramBoolean) {
//                                        // TODO Auto-generated method stub
//                                        if (paramBoolean) {
//                                                sv_llayout_close.setVisibility(View.GONE);
//                                                sv_llyout_start.setVisibility(View.VISIBLE);
//                                        }
//                                        else {
//                                                sv_llayout_close.setVisibility(View.VISIBLE);
//                                                sv_llyout_start.setVisibility(View.GONE);
//                                        }
//                                }
//
//                        });
                }
        }

        /**
         * 设置按钮状态
         *
         * @param which
         */
        private void setStatus(String which) {
                if ("sys".equals(which)) {
                        sv_systemvoice.setChecked(true);
                        sv_customvoice.setChecked(false);
                }
                else if ("custom".equals(which)) {
                        sv_systemvoice.setChecked(false);
                        sv_customvoice.setChecked(true);
                }

        }

        public void initView() {
                int TextRgb1 = android.graphics.Color.parseColor("#5170bb");
                int TextRgb2 = android.graphics.Color.parseColor("#d3d7d3");
                sv_customvoice = (ImageTextButtonInVc) findViewById(
                        R.id.sv_customvoice);
                sv_systemvoice = (ImageTextButtonInVc) findViewById(
                        R.id.sv_systemvoice);
                sv_systemvoice.setRes(new int[]{TextRgb1, TextRgb2},
                        new int[]{R.drawable.icon_rig_01, R.drawable.icon_rig_02});
                sv_customvoice.setRes(new int[]{TextRgb1, TextRgb2},
                        new int[]{R.drawable.icon_lei_02, R.drawable.icon_lei_01});
                sv_systemvoice.setText("系统音频");
                sv_customvoice.setText("自定义音频");

                tvTitle = (TextView) findViewById(R.id.tvTitle);
                btnComplete = (Button) findViewById(R.id.btnComplete);
                sv_return = (ImageButton) findViewById(R.id.sv_return);
//               sv_switch = (CheckBox) findViewById(R.id.sv_switch);
                sv_llyout_start = (LinearLayout) findViewById(R.id.sv_llyout_start);
                sv_llayout_close = (LinearLayout) findViewById(R.id.sv_llayout_close);
                btnComplete.setOnClickListener(this);
                sv_return.setOnClickListener(this);
                sv_systemvoice.setOnClickListener(new OnClickListener() {// 系统音频
                        @Override
                        public void onClick(View v) {


                                dialogLoading = LoadingDialogUtils.createLoadingDialog(StarttheVoiceActivity.this, getResources().getString(R.string.request));
                                dialogLoading.show();
                                HashMap<String, Object> hasMap = new HashMap<>();

                                if (from == null || from.equals("")) {
                                        // storeStatus("start_voice");
                                        // MsgSendUtils.sendStringMsg(MsgType.SEND_Setting_Custom_Voice,
                                        // "start_voice" );

//                   storeStatus(IntentKey.START_VOICE);
//                    MsgSendUtils.sendStringMsg(
//                            MsgType.Setting_Custom_Voice,
//                            IntentKey.START_VOICE);

                                        hasMap.put("postype", "start_voice");
//                    hasMap.put("postype","standby");
                                }
                                else {
//                    storeStatus(from);// 保存选中状态
//                    MsgSendUtils.sendStringMsg(MsgType.Setting_Custom_Voice,
//                            from);
                                        if (from.equals(IntentKey.SENSOR_HEAD)) {
                                                hasMap.put("postype", "sensor_head");
                                                Logger.e("save", "onSuccess:  postype :" + "http://" + IpMsgConst.SOCKET_HOST + hasMap.toString());
                                        }
                                        else if (from.equals(IntentKey.SENSOR_HEAD_BACK)) {
                                                hasMap.put("postype", "sensor_head_back");
                                        }
                                        else if (from.equals(IntentKey.SENSOR_LEFT_EAR)) {
                                                hasMap.put("postype", "sensor_left_ear");
                                        }
                                        else if (from.equals(IntentKey.SENSOR_RIGHT_EAR)) {
                                                hasMap.put("postype", "sensor_right_ear");
                                        }
                                        else if (from.equals(IntentKey.SENSOR_NECK)) {
                                                hasMap.put("postype", "sensor_neck");
                                        }
                                        else if (from.equals(IntentKey.SENSOR_BELLY)) {
                                                hasMap.put("postype", "sensor_belly");
                                        }
                                        else if (from.equals(IntentKey.SENSOR_BUTT)) {
                                                hasMap.put("postype", "sensor_butt");

                                        }else if (from.equals(IntentKey.SENSOR_PEOPLE)) {
                                                hasMap.put("postype", "sensor_people");

                                        }
                                }

                                hasMap.put("upload", "reset");
                                Logger.e("save", "onSuccess:upload" + "http://" + IpMsgConst.SOCKET_HOST + hasMap.toString());

                                OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "voicecustom", hasMap, new OkHttpUtils.OkHttpCallBack() {

                                        @Override
                                        public void onSuccess(String result) {

                                                JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                                Logger.e("save", "onSuccess:" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "strMsgBean.getRc()" + strMsgBean.getRc());
                                                if (strMsgBean.getRc() == 0) {
                                                        runOnUiThread(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                        storeStatus(from,"sys");
                                                                        dialogLoading.dismiss();
                                                                        ToastTools.newInstance(mContext).showText(getString(R.string.reset_success));
                                                                }
                                                        });
                                                }
                                                else {
                                                        runOnUiThread(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                        dialogLoading.dismiss();
                                                                }
                                                        });
                                                }
                                        }

                                        @Override
                                        public void onFailure() {
                                                Logger.e("save", "onFailure" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/");
                                                runOnUiThread(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                                dialogLoading.dismiss();
                                                                ToastTools.newInstance(mContext).showText(getString(R.string.network_request_failed));
                                                        }
                                                });
                                        }
                                });
                        }
                });
                sv_customvoice.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                Intent intent = new Intent(StarttheVoiceActivity.this,
                                        RecordActivity.class);
                                if (from == null || from.equals("")) {
                                        storeStatus("start_voice","custom");

                                        intent.putExtra("from", IntentKey.START_VOICE);
                                        // 从开机语模块跳转至音频选择模块
                                        intent.putExtra("fromActivity", ConstUtils.ACTIVITY_START_VOICE);
//                                        ((MainApplication) getApplication()).fromWitch = ConstUtils.ACTIVITY_START_VOICE;
                                }
                                else {
                                        storeStatus(from,"custom");

                                        intent.putExtra("from", from);
                                        if(IntentKey.SENSOR_PEOPLE.equals(from)){
                                                intent.putExtra("fromActivity", ConstUtils.ACTIVITY_SENSOR_PEOPLE);
                                        }else{
                                                // 从感应器模块跳转至音频选择模块
                                                intent.putExtra("fromActivity", ConstUtils.ACTIVITY_SENSOR);
//                                        ((MainApplication) getApplication()).fromWitch = ConstUtils.ACTIVITY_SENSOR;
                                        }
                                }
                                startActivity(intent);
                        }
                });
        }

        /**
         * 保存选择状态
         */
        private void storeStatus(String which,String type) {
                sv_systemvoice.setChecked(true);
                sv_customvoice.setChecked(false);
                SharedPreferences sp = getSharedPreferences(which,
                        Context.MODE_PRIVATE);
                Editor editor = sp.edit();
                editor.putString("which", type);
                editor.apply();
                /**
                 *
                 *
                 * 提交大U选择
                 */
        }

        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.sv_return:
                                finish();
                                break;
                        case R.id.btnComplete:
                                finish();
                                break;

                }

        }

}
